//
//  BHCircleServiceManager.swift
//  BigHealth
//
//  Created by 布丁 on 2021/8/3.
//

import UIKit

class BHCircleServiceManager: NSObject {
    
    var searchKey: String = ""
    
    // 固定栏目 && 非固定栏目 && 个人绑定的非固定栏目
    var circleColumnResponseForBlock:(([BHCircleColumnModel],[BHCircleColumnModel],[BHCircleColumnModel]) -> Void)?
    // 个人栏目
    var circleColumnResponseForUserBlock:(([BHCircleColumnModel]) -> Void)?
    // 绑定栏目的回调
    var circleBindColumnHandler:((Bool,String,[String]) -> Void)?
    // 获取栏目列表 成功失败  错误信息  总数 当前页数据
    var circleColumnListResponseBlock:((Bool,String,Int,[BHCircleListModel]) -> Void)?
    // 点赞或者取消赞之后的回调
    var circleLikeOrNotResponseBlock:((Bool,String) -> Void)?
    
}

//获取固定栏目和非固定栏目
extension BHCircleServiceManager {
    func requestColumnService()  {
        let columnManager = BHHTTPClient.init()
        columnManager.get(url: String.circle_column_url, paramters: ["type":""]) { (success, errorMsg, responseObject)  in
            
            var regularTemp: [BHCircleColumnModel] = []
            var unRegularTemp: [BHCircleColumnModel] = []
            var bindTemp: [BHCircleColumnModel] = []
            
            if success == true {
                let result = BHJsonTool.decodeArrayFromDic(dic: responseObject, key: "result")
                for item in result {
                    let model = BHCircleColumnModel.init()
                    model.parseDic(item as! [String:Any])
                    if model.fixedFlag == 1 {
                        regularTemp.append(model)
                    }else {
                        if model.userColumnFlag == 1 { // 用户已关联
                            bindTemp.append(model)

                        }else {
                            unRegularTemp.append(model)
                        }
                    }
                }
            }else {
                BHToast.toast(text: errorMsg)
            }
            self.circleColumnResponseForBlock?(regularTemp,unRegularTemp,bindTemp)
        }
    }
}

// 获取个人栏目
extension BHCircleServiceManager {
    func requestSelfColumnService()  {
        let selfManager = BHHTTPClient.init()
        selfManager.get(url: String.circle_personalColunm_url, paramters: nil) { (success, errorMsg, responseObject)  in
            
            var selfTemp: [BHCircleColumnModel] = []

            if success == true {
                let result = BHJsonTool.decodeArrayFromDic(dic: responseObject, key: "result")
                for item in result {
                    let model = BHCircleColumnModel.init()
                    model.parseDic(item as! [String:Any])
                    selfTemp.append(model)
                }
            }else {
                BHToast.toast(text: errorMsg)
            }
            self.circleColumnResponseForUserBlock?(selfTemp)
        }
    }
}

// 绑定栏目
extension BHCircleServiceManager {
    func bindColumnService(columnIdArray: [String])  {
        let bindManager = BHHTTPClient.init()
        bindManager.post(url: String.circle_relateColumn_url, paramters: ["columnIdList":columnIdArray]) { (success, errorMsg, responseDic) in
            self.circleBindColumnHandler?(success,errorMsg,columnIdArray)
        }
    }
}

extension BHCircleServiceManager {
    func loadCircleListService(columId: String?,page: Int) {
        var dic: [String: Any] = [:]
        dic["pageNo"] = page
        dic["pageSize"] = 20
        if columId != nil {
            if columId! == "推荐" {
                dic["sortType"] = "1"
            }else {
                dic["circleColumnId"] = columId!
            }
        }
       
        dic["search"] = self.searchKey
        let manager = BHHTTPClient.init()
        manager.get(url: String.circle_main_list_url, paramters: dic) { (success, errorMsg, responseDic) in
         
            var tempArray: [BHCircleListModel] = []
            var total: Int = 0
            
            if success == true {
                let result = BHJsonTool.decodeDicFromDic(dic: responseDic, key: "result")
                total = BHJsonTool.decodeIntFromDic(dic: responseDic, key: "total")
                let record = BHJsonTool.decodeArrayFromDic(dic: result, key: "records")
               
                for item in record {
                    let model = BHCircleListModel.init()
                    model.parseDic(item as! [String:Any])
                    tempArray.append(model)
                }
             
            }else {
                BHToast.toast(text: errorMsg)
            }
            self.circleColumnListResponseBlock?(success,errorMsg,total,tempArray)
        }
    }
    
    func circleLikeOrNot(model: BHCircleListModel)  {
        SVProgressHUD.show()
        BHCommonService.circleLikeOrNot(circleId: model.circleId) { (success, errorMsg) in
            SVProgressHUD.dismiss()
            if success == true{
                if model.likeFlag == 1 { // 已经是点赞了，此时取消
                    model.likeFlag = 0
                    model.supportNumber -= 1
                }else {
                    model.likeFlag = 1
                    model.supportNumber += 1
                }
             
            }else {
                BHToast.toast(text: errorMsg)
            }
            
            self.circleLikeOrNotResponseBlock?(success,errorMsg)
        }
    }
}
